﻿var VideoNetSchool = {
    Teacher: {},
    Course: {},
    User: {},
    SVR: {},
    Calendar: {}

};
var rond=1;
var InitPostUrl='../Handler/Handler.ashx';
VideoNetSchool.Teacher = {
    EnableTeacher: function(id, type) {
        VideoNetSchoolCore.Form.PostData(
                InitPostUrl,
                { OpType: "Modify", PostData: jsonDataString, strValue: surepwd },
                function(e) {
                    if (e == "True") {
                        alert("成功修改");
                    } else {
                        alert("修改失败，请联系管理员！");
                        return;
                    }
                }
            );
    }


};
VideoNetSchool.Course = {
    ChkCourse: function(OpType) {
        var className = $("[DataField='ClassName']").val();
        var classRoom = $("[DataField='ClassRoom']").val();
        var classTeacher = $("[DataField='ClassTeacher']").val();
        var classCourse = $("[DataField='ClassCourse']").val();
        var classStartDate = $("[DataField='ClassBeginDate']").val();
        var classEndDate = $("[DataField='ClassEndDate']").val();
        var classBeginTime = $("[DataField='ClassBeginTime']").val();
        var classEndTime = $("[DataField='ClassEndTime']").val();
        var id = $("#hfId").val();
        VideoNetSchoolCore.Form.PostData(
                InitPostUrl,
                { OpType: OpType, ID: id, ClassName: className, ClassRoom: classRoom, ClassTeacher: classTeacher, ClassCourse: classCourse, ClassBeginDate: classStartDate, ClassEndDate: classEndDate, ClassBeginTime: classBeginTime, ClassEndTime: classEndTime },
                function(e) {

                    if (e == 1) {
                        alert("填入数据有重复，请检查！");
                        return;
                    }
                    if (e == 2) {
                        alert("数据添加成功！");
                        window.location.href = "../Manager/SearchCourseAllList.aspx";
                    }
                    if (e == -1) {
                        alert("数据保存失败，请检查！");
                    }
                    if (e == "success") {
                        alert("数据修改成功！");
                        window.location.href = "../Manager/SearchCourseAllList.aspx";
                    }




                }
            );

    },
    AddRow: function() {
        rond = rond + 1;
        var obj = $("#row").clone(true).appendTo("#list");
        var tabDetail = $("#list");
        obj.find("[DataField='TrainClassName']").attr({ id: "txtTrainClassName" + rond });
        obj.find("[DataField='TrainClassID']").attr({ id: "HidTrainClassID" + rond });
        obj.find("[DataField='VC_name']").attr({ id: "txtVC_name" + rond });
        obj.find("[DataField='VC_desc']").attr({ id: "txtVC_desc" + rond });
        obj.find("[DataField='Vc_content']").attr({ id: "txtcontent" + rond });
        obj.find("[DataField='Vc_targetUsers']").attr({ id: "txttargetUsers" + rond });
        tabDetail.find("[DataField='rowhead']").attr({ id: "row" + rond });
        obj.show();
        //  alert(rond);

        //        $("[DataField='TrainClassName']").attr({ id: "txtTrainClassName" + rond });
        $("#txtTrainClassName" + rond).autocomplete("../Handler/ClassComplete.ashx", {
            minChars: 0,
            formatItem: function(data, i, n) {
                var array = data.toString().split(',');
                return array[0];
            },
            formatResult: function(data, i, n) {
                var array = data.toString().split(',');
                return array[1];
            }

        }).result(function(event, data, formatted) {
            var array = data.toString().split(','); 
            $("#HidTrainClassID" + event.currentTarget.id.substring(event.currentTarget.id.length - 1, event.currentTarget.id.length)).val(array[2]);

        });


    },
    PostCourse: function() {
        var tabDetail = $("#list");

        var attendenceData = new Array();
        var trs = $("#list").find("tr");
        if (trs.length <= 1) {
            alert("没有数据填写！");
            return;
        }
        var flg = false;
        var ff = false;
        trs.each(function(i) {

            if (!(i == 0)) {
                if (tabDetail.find("[DataField='TrainClassID']").length > 0) {
                    var jsonData = VideoNetSchoolCore.Form.GetDivFormData($(this));

                    if (jsonData["TrainClassID"] != 0) {
                        attendenceData.push(jsonData);
                    } else {

                        flg = true;

                    }
                    if (jsonData["VC_name"] == "") {
                        ff = true;
                    }

                }
            }
        });
//        alert($.toJSON(attendenceData));
//        return;
        if (flg) {
            alert("对不起，培训班号不存在，请检查培训班是否输入正确！");
            return;
        }
        if (ff) {
            alert("对不起，请检查是否有课程名没有填写！");
            return;
        }
        var jsonDataString = $.toJSON(attendenceData);

        VideoNetSchoolCore.Form.PostData(
            InitPostUrl,
            { OpType: "SaveCourses", PostData: jsonDataString },
           function(e) {
               if (e == "False") {

                   alert("对不起，培训班号不存在，请检查培训班是否输入正确！");
                   return;
               }

               if (e > 0) {
                   alert("保存成功！");
                   window.location.href = "../Manager/SearchCourseList.aspx";
               } else {
                   alert("保存失败，请检查！");
               }

           }
        );


    }
};
VideoNetSchool.User = {
    SaveUser: function() {
        var tabDetail = $("#list");
        var attendenceData = new Array();
        var trs = $("#list").find("tr");
        if (trs.length <= 1) {
            alert("没有数据填写！");
            return;
        }
        trs.each(function(i) {
            if ($(this).find("[DataField='VU_realname']").length > 0) {
                var jsonData = VideoNetSchoolCore.Form.GetDivFormData($(this));

                if (jsonData["VU_realname"] != 0) {
                    attendenceData.push(jsonData);
                }
            }
        });
        var jsonDataString = $.toJSON(attendenceData);

        VideoNetSchoolCore.Form.PostData(
                InitPostUrl,
                { OpType: "SaveUser", PostData: jsonDataString },
                function(e) {
                    if (e == "Success") {
                        alert("保存成功！");
                        window.location.href = "../Manager/ViewUserInfoList.aspx";
                    } else {
                        alert("保存失败，请检查！");
                    }

                }
            );
    },
    AddRow: function() {
        rond = rond + 1;
        var obj = $("#row").clone(true).appendTo("#list");
        obj.find("[DataField='VU_loginname']").attr({ id: "txtLoginName" + rond });
        obj.find("[DataField='spUsers']").attr({ id: "spUser" + rond });
        obj.find("[DataField='VU_mobile']").attr({ id: "txtMobile" + rond });
        obj.find("[DataField='txtEmail']").attr({ id: "VU_email" + rond });
        obj.find("[DataField='VU_loginname']").attr({ id: "txtUserRealName" + rond });
        $("#list").find("[DataField='rowhead']").attr({ id: "row" + 1 });
        obj.show();


    },
    CheckUser: function(id, value) {

        if (value == "") {
            alert("请填写登录名！");
            $("#" + id).focus();
            return;
        }
        VideoNetSchoolCore.Form.PostData(
                InitPostUrl,
                { OpType: "CheckUser", PostData: value },
                function(e) {

                    if (e == 1) {
                        alert("此登录名已经存在");
                        return;
                    }

                }
            );

    },
    CheckUserEmail: function(id) {

        //    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

        //    if (!myreg.test($("#" + id).val())) {
        //        alert("对不起，你输入的邮箱格式不正确！请检查！");
        //        return;
        //    }

    }
};
VideoNetSchool.SVR = {
    ChkPort: function() {
        var svrport = $("[DataField='SvrPort']").val();
        var svrId = $("[DataField='SVRList']").val();
        VideoNetSchoolCore.Form.PostData(
                InitPostUrl,
                { OpType: "CheckSVR", PostData: svrport, strValue: svrId },
                function(e) {

                    if (e != "") {
                        alert(e);
                        return false;
                    }

                }
            );

    }
};
VideoNetSchool.Calendar = {
    LoadClass: function(CurrentIndex, CourseID, CourseDate, RoomId, StartTime, EndTime) {

        //        VideoNetSchool.Calendar.LoadComment(CourseID, 1, pageSize);
        VideoNetSchoolCore.Form.PostData(
                "../Handler/CalendarHandler.ashx",
                { OpType: "CalendarCourseList", CurrentIndex: CurrentIndex, CourseID: CourseID, CourseDate: CourseDate, RoomId: RoomId, StartTime: StartTime, EndTime: EndTime },
                function(e) {
                    if (e.ResultMessage != "") {
                        $("#Player").html(e.ResultMessage);

                    }
                    $("#CourseDesc").html(e.CourseDesc);
                    $("#TeacherName").html(e.TeacherName);
                    $("#CourseName").html(e.CourseName);
                    $("#classInfo").show(300);
                    $("#clsInfo").removeClass("icon_collapse").addClass("icon_expand");
                    $("#hfPageCount").val(e.RtnCount);
                    
                    var time = StartTime + "-" + EndTime;
                    $("#spTime").empty().html(CourseDate);

                    if (e.ReturnValue == "" || e.ReturnValue == null) {
                        $("#v").html("<div id='container'></div><span  id='spanPlayer'><embed src=\"../Plugin/loading.swf\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" wmode=\"opaque\" width=\"450\" height=\"280\"></embed></span>");
                        alert("对不起，当前第" + parseInt(parseInt(CurrentIndex) + 1) + "段视频正在转码...");
                        return;
                    }
                    if (e.ReturnValue != "") {

                        $("#v").html("<div id='container'></div><span  id='spanPlayer'></span>");
                        $("#spanPlayer").html(e.ReturnValue);
                        return;


                    }


                }
            );

    },



    PreWeek: function() {

        var CourselistID = $("[DataField='CourselistID']").val();
        var CourseDate = $("[DataField='CourseDate']").val();
        var RoomId = $("[DataField='RoomId']").val();
        var StartTime = $("[DataField='StartTime']").val();
        var EndTime = $("[DataField='EndTime']").val();
        var CurrentMinY = $("[DataField='CurrentMinY']").val();
        var CurrentMinMon = $("[DataField='CurrentMinMon']").val();
        var CurrentMinDay = $("[DataField='CurrentMinDay']").val();

        VideoNetSchoolCore.Form.PostData(
                "../Handler/CalendarHandler.ashx",
                { OpType: "PreWeek", CourseID: CourselistID, CourseDate: CourseDate, RoomId: RoomId, StartTime: StartTime, EndTime: EndTime, CurrentY: CurrentMinY, CurrentM: CurrentMinMon, CurrentMd: CurrentMinDay },
                function(e) {
                    if (e != "") {
                        $("#spCourseList").empty().html(e.ResultMessage);
                        $("[DataField='CurrentMinY']").val(e.RtnMinYear);
                        $("[DataField='CurrentMinMon']").val(e.RtnMinMonth);
                        $("[DataField='CurrentMinDay']").val(e.RtnMinDay);
                        $("[DataField='CurrentMaxY']").val(e.RtnMaxYear);
                        $("[DataField='CurrentMaxMon']").val(e.RtnMaxMonth);
                        $("[DataField='CurrentMaxDay']").val(e.RtnMaxDay);

                    }

                }
            );
    },
    NextWeek: function() {
        var CourselistID = $("[DataField='CourselistID']").val();
        var CourseDate = $("[DataField='CourseDate']").val();
        var RoomId = $("[DataField='RoomId']").val();
        var StartTime = $("[DataField='StartTime']").val();
        var EndTime = $("[DataField='EndTime']").val();
        var CurrentMaxY = $("[DataField='CurrentMaxY']").val();
        var CurrentMaxMon = $("[DataField='CurrentMaxMon']").val();
        var CurrentMaxDay = $("[DataField='CurrentMaxDay']").val();
        VideoNetSchoolCore.Form.PostData(
                 "../Handler/CalendarHandler.ashx",
                { OpType: "NextWeek", CourseID: CourselistID, CourseDate: CourseDate, RoomId: RoomId, StartTime: StartTime, EndTime: EndTime, CurrentY: CurrentMaxY, CurrentM: CurrentMaxMon, CurrentMd: CurrentMaxDay },
                function(e) {
                    if (e != "") {
                        $("#spCourseList").empty().html(e.ResultMessage);
                        $("[DataField='CurrentMinY']").val(e.RtnMinYear);
                        $("[DataField='CurrentMinMon']").val(e.RtnMinMonth);
                        $("[DataField='CurrentMinDay']").val(e.RtnMinDay);
                        $("[DataField='CurrentMaxY']").val(e.RtnMaxYear);
                        $("[DataField='CurrentMaxMon']").val(e.RtnMaxMonth);
                        $("[DataField='CurrentMaxDay']").val(e.RtnMaxDay);
                    }

                }
            );
    },
    Submit: function() {

        var SstatTime = $("[datafield='SStartTime']").val();
        var SEndTime = $("[datafield='SEndTime']").val();
        var Vsid = $("#vSid").val();

        if (Vsid == "") {
            alert("对不起你没有选择课节！");
            return;
        }
        if (SstatTime == "") {
            alert("对不起，请输入开始时间！");
            return;
        }
        if (SEndTime == "") {
            alert("对不起，请输入结束时间！");
            return;
        }

        if (VideoNetSchool.Calendar.ConvertTimeToChk(SstatTime) > VideoNetSchool.Calendar.ConvertTimeToChk(SEndTime)) {
            alert("对不起，开始时间不能大于结束时间！");
            return;
        }

        var r = confirm("当前视频的开始时间：" + SstatTime + ",结束时间：" + SEndTime);

        if (r == true) {
            $("#loading").show();
            $("#btnSubmit").hide();
            VideoNetSchoolCore.Form.PostData(
                 "../Handler/Handler.ashx",
                { OpType: "SaveFormatTime", PostData: SstatTime, strValue: VideoNetSchool.Calendar.ConvertTimeToChk(SEndTime) - VideoNetSchool.Calendar.ConvertTimeToChk(SstatTime), Vsid: Vsid },
                function(e) {
                if (e == 1) {
                        $("#v").html("<div id='container'></div><span  id='spanPlayer'><embed src=\"../Plugin/loading.swf\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" wmode=\"opaque\" width=\"450\" height=\"280\"></embed></span>");
                        alert("数据提交成功！");
                        $("#btnSubmit").show();
                        $("#loading").hide();
                        return;
                    }
                    if (e == 2) {
                        alert("对不起，当前课节正在转码，请等待！");
                        $("#btnSubmit").show();
                        $("#loading").hide();
                        return;
                    }

                }
            );
        } else {
            $("#loading").hide();
        }
    },
    ConvertTimeToChk: function(time) {
        var fen = time.split(":");
        var len = parseInt(fen[0] * 60) + parseInt(fen[1]);
        return len;
    }

};
